using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework;

namespace gears
{
    class Tutorial
    {
       private Texture2D tutorial_button;
       private Texture2D prev_button;
       private Texture2D next_button;
       private Texture2D close_button;
       private Texture2D objective_button;
       private List<Texture2D> tutorial_pages = new List<Texture2D>();

       private Vector2 tutorial_button_position;
       private Vector2 prev_button_position;
       private Vector2 next_button_position;
       private Vector2 close_button_position;
       private Vector2 tutorial_page_position;
       private Vector2 objective_button_position;

       private bool tutorial_actived=false;
       private bool single_tutorial = true;
       private int current_tutorial_page=0;

       private bool tap_enable = true;

       public static bool objective_enabled = false;
       public static bool tutorial_enabled = false;
       public static bool single_objective_enabled = false;

        public Tutorial()
        {
            single_tutorial = false;
            tutorial_button = ContentLoader.LoadTexture("tutorial_button");
            prev_button = ContentLoader.LoadTexture("left_control");
            next_button=ContentLoader.LoadTexture("right_control");
            close_button = ContentLoader.LoadTexture("cancel_tutorial");

            tutorial_pages.Add(ContentLoader.LoadTexture("tutorial_page1"));
            tutorial_pages.Add(ContentLoader.LoadTexture("tutorial_page2"));
            tutorial_pages.Add(ContentLoader.LoadTexture("tutorial_page3"));
            tutorial_pages.Add(ContentLoader.LoadTexture("tutorial_page4"));

            tutorial_button_position = new Vector2(0, 0);
            tutorial_page_position = new Vector2(720 - tutorial_pages[0].Width,0+tutorial_button.Height);
            prev_button_position = new Vector2(tutorial_page_position.X, tutorial_page_position.Y+tutorial_pages[0].Height);
            next_button_position = new Vector2(tutorial_page_position.X+tutorial_pages[0].Width-next_button.Width, tutorial_page_position.Y + tutorial_pages[0].Height);
            close_button_position = new Vector2(tutorial_page_position.X + tutorial_pages[0].Width/2-close_button.Width/2, tutorial_page_position.Y + tutorial_pages[0].Height-30);

        }
        public Tutorial(Texture2D tutorial_page)
        {
            tutorial_pages.Add(tutorial_page);
            close_button = ContentLoader.LoadTexture("cancel_tutorial");
            objective_button = ContentLoader.LoadTexture("objective_button");

            tutorial_page_position = new Vector2(720 - tutorial_pages[0].Width,50);
            close_button_position = new Vector2(tutorial_page_position.X + tutorial_pages[0].Width / 2 - close_button.Width / 2, tutorial_page_position.Y + tutorial_pages[0].Height-30);
            objective_button_position = new Vector2(800 - objective_button.Width, 0);
            single_tutorial = true;
        }
      
        public void UpdateTutorial()
        {

             CheckClickToButtons();

             if (tutorial_actived == true)
             {
                 Player.PlayerActivity = false;
             }
           
        }
        private void CheckClickToButtons()
        {   
            var tap_position=TapHandler.GetTouches();
            if (tap_position.Count > 0)
            {
                if (tap_enable == true)
                {
                    tap_enable = false;

                    Rectangle tap_rectangle = new Rectangle((int)tap_position[0].X, (int)tap_position[0].Y, 5, 5);

                    if (single_tutorial == false && objective_enabled==false)
                    {
                        Rectangle rect = new Rectangle((int)tutorial_button_position.X, (int)tutorial_button_position.Y, tutorial_button.Width, tutorial_button.Height);
                        if (tap_rectangle.Intersects(rect))
                        {
                            tutorial_actived = true;
                            Player.PlayerActivity = false;
                            tutorial_enabled = true;
                        }
                        Rectangle prev = new Rectangle((int)prev_button_position.X, (int)prev_button_position.Y, prev_button.Width, prev_button.Height);
                        if (tap_rectangle.Intersects(prev))
                        {
                            if (current_tutorial_page > 0)
                            {
                                current_tutorial_page--;
                            }
                            else
                            {
                                current_tutorial_page = tutorial_pages.Count - 1;
                            }
                        }
                        Rectangle next = new Rectangle((int)next_button_position.X, (int)next_button_position.Y, next_button.Width, next_button.Height);
                        if (tap_rectangle.Intersects(next))
                        {
                            if (current_tutorial_page < tutorial_pages.Count - 1)
                            {
                                current_tutorial_page++;
                            }
                            else
                            {
                                current_tutorial_page = 0;
                            }
                        }
                    }
                    Rectangle close = new Rectangle((int)close_button_position.X, (int)close_button_position.Y, close_button.Width, close_button.Height);
                    if (tap_rectangle.Intersects(close))
                    {
                        if (objective_enabled == false)
                        {
                            if (single_objective_enabled == false)
                            {
                                Player.PlayerActivity = true;
                            }
                            tutorial_enabled = false;
                        }
                        else
                        {
                            objective_enabled = false;
                        }
                        tutorial_actived = false;
                     
                    }
                    if (single_tutorial == true && tutorial_enabled==false)
                    {
                        Rectangle objective = new Rectangle((int)objective_button_position.X, (int)objective_button_position.Y, objective_button.Width, objective_button.Height);
                        if (tap_rectangle.Intersects(objective))
                        {

                            tutorial_actived = true;
                            objective_enabled = true;

                        }
                    }
                }
            }
            else
            {
                tap_enable = true;
            }
        }
        public void DrawTutorial(SpriteBatch sprite_batch)
        {
            if (single_tutorial == false)
            {
                if (tutorial_actived == false)
                {
                    sprite_batch.Draw(tutorial_button, tutorial_button_position, Color.White);
                }
                else
                {
                    sprite_batch.Draw(tutorial_button, tutorial_button_position, Color.Green);
                }

                if (tutorial_actived == true)
                {
                    sprite_batch.Draw(prev_button, prev_button_position, Color.White);
                    sprite_batch.Draw(next_button, next_button_position, Color.White);
                    sprite_batch.Draw(close_button, close_button_position, Color.White);
                    sprite_batch.Draw(tutorial_pages[current_tutorial_page], tutorial_page_position, Color.White);

                }
            }
            else if(tutorial_actived==true)
            {
                sprite_batch.Draw(tutorial_pages[current_tutorial_page], tutorial_page_position, Color.White);
                sprite_batch.Draw(close_button, close_button_position, Color.White);
               
            }
            if (single_tutorial == true)
            {
                if (objective_enabled == false)
                {
                    sprite_batch.Draw(objective_button, objective_button_position, Color.White);
                }
                else
                {
                    sprite_batch.Draw(objective_button, objective_button_position, Color.Green);
                }
            }
        }

        public bool TutorialActivity
        {
            get
            {
                return tutorial_actived;
            }
            set
            {
                tutorial_actived = value;
            }
        }
    }
}
